package edu.asu.sefcom.crimenet.search.data;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import edu.asu.sefcom.crimenet.search.util.Util;

public class RelationalDBConnector extends SqlMapClientDaoSupport implements
		DataConnector {

	Logger logger = Logger.getLogger(RelationalDBConnector.class);
	
	public void insert(UserPostData user) {
		// int i = getSqlMapClientTemplate().delete(
		// "edu.asu.sefcom.crimenet.search.data.UserPostData.delete", user);

		Object r = getSqlMapClientTemplate().insert(
				"edu.asu.sefcom.crimenet.search.data.UserPostData.insert", user);
//		System.out.println("  Stored object success");
		logger.error("  Stored object success");
	}

	public void update(UserPostData user) {
		int retval = getSqlMapClientTemplate().update(
				"edu.asu.sefcom.crimenet.search.data.UserPostData.update", user);
	}

	public UserPostData loadById(String id) {
		UserPostData param = new UserPostData();
		param.setIdPost(Integer.valueOf(id));
		return (UserPostData) getSqlMapClientTemplate().queryForObject(
				"edu.asu.sefcom.crimenet.search.data.UserPostData.getPostData", param);
	}

	public List<UserPostData> getUserInfos(String parentuser, String username) {
		Map<String, String> mapInfo = new HashMap<String, String>();
		boolean hasparent = false;
		if (null != parentuser) {
			mapInfo.put("PARENTUSER", parentuser);
			hasparent = true;
		}

		if (null != username) {
			mapInfo.put("USERNAME", username);
		}

		List<UserPostData> userPostData = null;
		if (null != parentuser && null != username) {
			userPostData = (List<UserPostData>) getSqlMapClientTemplate()
					.queryForList(
							// "edu.asu.sefcom.crimenet.search.data.UserPostData.getAll",
							// mapInfo);
							"edu.asu.sefcom.crimenet.search.data.UserPostData.getPostDataWithUserNameAndParent",
							mapInfo);
		} else {
			userPostData = (List<UserPostData>) getSqlMapClientTemplate()
					.queryForList(
							// "edu.asu.sefcom.crimenet.search.data.UserPostData.getAll",
							// mapInfo);
							"edu.asu.sefcom.crimenet.search.data.UserPostData.getPostDataWithParent",
							mapInfo);
		}
		return userPostData;
	}

	@Override
	public void storeContent(Map<String, String> mapInfo, List<String> content) {
		// TODO Auto-generated method stub
		String filename = mapInfo.get("FILENAME");
		String username = mapInfo.get("USER");

		for (String postcontent : content) {
			UserPostData data = new UserPostData();
			data.setUserName(username);
			data.setPostContent(postcontent);
			data.setFileName(filename);
			data.setParentUser("bhcrew");
			insert(data);
		}
	}

	@Override
	public void removeContent(Map<String, String> mapInfo) {
		// TODO Auto-generated method stub
		String filename = mapInfo.get("FILENAME");
		String username = mapInfo.get("USER");
		int i = getSqlMapClientTemplate().delete(
				"edu.asu.sefcom.crimenet.search.data.UserPostData.deleteFile", mapInfo);
		logger.error("  deleted return val : " + i);
	}

	public int getOutlinksCountForUser(String parentuser, String username) {
		// TODO Auto-generated method stub
		Map<String, String> mapInfo = new HashMap<String, String>();
		mapInfo.put("PARENTUSER", parentuser);
		mapInfo.put("USERNAME", username);
		int count = (Integer) getSqlMapClientTemplate()
				.queryForObject(
						"edu.asu.sefcom.crimenet.search.data.UserPostData.getPostData",
						mapInfo);
		return count;
	}

	public void updateArticlesOutlinks(String parentuser, String username,
			String postStart, String postEnd) {
		// TODO Auto-generated method stub
		Map<String, String> mapInfo = new HashMap<String, String>();
		mapInfo.put("PARENTUSER", parentuser);
		mapInfo.put("USERNAME", username);
		List<UserPostData> userPostDatas = (List<UserPostData>) getSqlMapClientTemplate()
				.queryForList(
						"edu.asu.sefcom.crimenet.search.data.UserPostData.getPostDataWithParent",
						mapInfo);
		for (UserPostData userPostData : userPostDatas) {
			// ""==userPostData.get
		}
	}

	public void updateEmptyArticles(String parentuser, String username) {
		// TODO Auto-generated method stub
		Map<String, String> mapInfo = new HashMap<String, String>();
		mapInfo.put("PARENTUSER", parentuser);
		mapInfo.put("USERNAME", username);
		List<UserPostData> userPostDatas = (List<UserPostData>) getSqlMapClientTemplate()
				.queryForList(
						"edu.asu.sefcom.crimenet.search.data.UserPostData.getPostDataWithParent",
						mapInfo);
		for (UserPostData userPostData : userPostDatas) {
			String det = userPostData.getArticle();
			if (null == det || "" == det) {
				String result = Util.html2text(userPostData.getPostContent());
				StringBuffer sb = new StringBuffer();
				Pattern p = Pattern.compile("\\W");
				for (char ch : result.toCharArray()) {
					Matcher m = p.matcher("" + ch);
					result = m.group();
					sb.append(result);
				}
				userPostData.setArticle(sb.toString());
			}
		}
	}

	public void storeContent(String parentname, String username, String icq,
			String email, String country, String filename,
			List<String> resultList) {
		for (String postcontent : resultList) {
			UserPostData data = new UserPostData();
			data.setUserName(username);
			data.setPostContent(postcontent);
			data.setFileName(filename);
			data.setParentUser(parentname);
			data.setIcq(icq);
			data.setEmail(email);
			data.setCountry(country);
			insert(data);
		}		
	}

}
